<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .headline {
            overflow: hidden;
            height: 50px;
            background-color: white;
            border-top: 5px solid pink;
            border-bottom: 1px solid #eedef0;
            display: flex; /* 使用flex布局 */
            align-items: center; /* 垂直居中对齐 */
        }
        .headline a {
            color: #4c4c4c;
            text-decoration: none;
            padding: 0 10px; /* 使用padding简化定义 */
            height: 50px;
            line-height: 50px;
            text-align: center;
            font-size: 12px;
        }
        .login {
            margin-left: auto; /* 将登录链接推到右侧 */
        }
        .headline a:hover {
            background-color: #4c4c4c;
            color: white;
            transition: background-color 0.6s ease, color 0.5s ease;
            opacity: 0.6; /* 背景色在0.5秒内以ease速度曲线过渡 */
        }
        .search {
            width: 412px;
            height: 30px;
            border: 1px solid #00a4ff;
            margin-left: 59px;
            display: flex; /* 修改为flex布局，简化内部排版 */
        }
        .search input {
            flex: 1; /* 输入框占据剩余空间 */
            height: 28px;
            border: 0;
            padding-left: 20px;
            font-size: 14px;
        }
        .search button {
            height: 30px; /* 按钮高度与输入框一致 */
        }
    </style>
</head>
<body>
<div class="headline">
    <a href="/myweb/selectAllByUserName">购物车</a>
    <a href="https://www.google.com">google（搜索引擎）</a>
    <a href="https://www.bilibili.com">bilibili（电商平台）</a>
    <a href="https://www.jd.com">京东（电商平台）</a>

    <!-- 搜索表单 -->
    <form action="/myweb/selectByBrandName" method="post" class="search-form">
        <input type="text" id="brandname" name="brandname" placeholder="请输入搜索内容" required>
        <button type="submit">搜索</button>
    </form>

    <a class="login" href="login.html">登录</a>
    <a class="login" href="register.html">注册</a>
</div>
<hr>
<table border="1" cellspacing="0" width="80%">
    <tr>
        <th>序号</th>
        <th>商品名称</th>
        <th>企业名称</th>
        <th>价格</th>
        <th>介绍</th>
        <th>操作</th>
    </tr>

    <c:forEach items="${brands}" var="brand" varStatus="status">
        <tr align="center">
            <td>${status.count}</td>
            <td>${brand.brandName}</td>
            <td>${brand.companyName}</td>
            <td>${brand.price}</td>
            <td>${brand.description}</td>
            <td>
                <a href="javascript:void(0);" onclick="addBuy('${brand.brandName}')">添加</a>
            </td>
        </tr>
    </c:forEach>

</table>

<script>
    function addBuy(goodname) {
        var encodedName = encodeURIComponent(goodname);

        // 使用 AJAX 发送请求
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "/myweb/addBuy?goodname=" + encodedName, true);

        xhr.onreadystatechange = function() {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                if (xhr.status === 200) {
                    // 请求成功后，可以在这里进行一些操作，例如提示用户
                    alert("商品已添加！");
                } else {
                    // 处理请求错误
                    alert("添加失败，请重试。");
                }
            }
        };

        xhr.send();
    }

    document.getElementById("add").onclick = function () {
        location.href = "/myweb/addBrand.jsp";
    }
</script>

</body>
</html>
